package commands;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.newdawn.slick.command.BasicCommand;

import com.animiku.first.smartdashboardlib.SmartDashboard;

import desktop.SuperDashboard;

public class RobotCommand {
	private static final String COMMAND_PREFIX = "Command_";
	private static final int NUM_COMMAND_SLOTS = 10;
	private enum DATA_TYPE { INT, DOUBLE, STRING };
	
	private static final List<RobotCommand> COMMANDS = new ArrayList();
	private static final List<String> QUEUED_COMMANDS = new ArrayList();
	private static SmartDashboard dashboard;
	
	public static void initiate(SmartDashboard dash) throws IOException {
		dashboard = dash;
		BufferedReader reader = new BufferedReader(new FileReader("CommandList.txt"));
		String line = reader.readLine();
		while(line != null) {
			if(!line.startsWith("!")) {
				Scanner scanner = new Scanner(line);
				String name = scanner.next();
				DATA_TYPE[] arguments = new DATA_TYPE[scanner.nextInt()];
				for(int a = 0; a < arguments.length; a++) {
					String type = scanner.next();
					if(type.equalsIgnoreCase("i")) {
						arguments[a] = DATA_TYPE.INT;
					} else if(type.equalsIgnoreCase("f")) {
						arguments[a] = DATA_TYPE.DOUBLE;
					} else if(type.equalsIgnoreCase("s")) {
						arguments[a] = DATA_TYPE.STRING;
					}
				}
				COMMANDS.add(new RobotCommand(name, arguments));
			}
			line = reader.readLine();
		}
		for(int a = 0; a < NUM_COMMAND_SLOTS; a++) {
			dashboard.putString(SuperDashboard.IDENTIFIER + COMMAND_PREFIX + a, "");
		}
	}
	
	public static void update() {
		for(int a = 0; a < NUM_COMMAND_SLOTS; a++) {
			String field = dashboard.getString(SuperDashboard.IDENTIFIER + COMMAND_PREFIX + a);
			
		}
	}
	
	private String name;
	private DATA_TYPE[] arguments;
	
	public RobotCommand(String name, DATA_TYPE[] arguments) {
		this.name = name;
		this.arguments = arguments;
	}
	
	public void run(Object[] args) {
		String commandField = name + " ";
		if(args.length != arguments.length) {
			throw new IllegalStateException("Number of arguments does not match command format.");
		}
		for(int a = 0; a < args.length; a++) {
			if(arguments[a] == DATA_TYPE.INT) {
				if(!(args[a] instanceof Integer)) {
					throw new IllegalStateException("Type of argument " + a + " does not match command format.");
				}
				commandField += (int) args[a] + " ";
			} else if(arguments[a] == DATA_TYPE.DOUBLE) {
				if(!(args[a] instanceof Double)) {
					throw new IllegalStateException("Type of argument " + a + " does not match command format.");
				}
				commandField += ((double) args[a]) + " ";
			} else if(arguments[a] == DATA_TYPE.STRING) {
				if(!(args[a] instanceof String)) {
					throw new IllegalStateException("Type of argument " + a + " does not match command format.");
				}
				commandField += ((String) args[a]) + " ";
			}
		}
		QUEUED_COMMANDS.add(commandField);
	}
}
